package cn.mastor.utils;

import com.alibaba.fastjson.JSONObject;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.URL;
import java.net.URLConnection;
import java.util.LinkedHashMap;

public class StartMixStream {

    // 发送post请求
    public static String sendPost(String url, String param) {
        String result = "";
        try {
            URL realUrl = new URL(url);
            URLConnection conn = realUrl.openConnection();
            conn.setDoOutput(true);
            conn.setDoInput(true);
            try (
                PrintWriter out = new PrintWriter(conn.getOutputStream())
            ){
                out.print(param);
                out.flush();
            }
            try (
                BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"))
            ){
                String line;
                while ((line = in.readLine()) != null) {
                    result += "\n" + line;
                }
            }
        } catch (Exception e) {
            System.out.println(e);
            e.printStackTrace();
        }
        return result;
    }


    //构造mixinput的数据(数组)
    public static LinkedHashMap[] getMixInputData_array(String inputStreamId1, String inputStreamId2){

        LinkedHashMap<String,Object> mixInput_stream1_rect_map = new LinkedHashMap<>();
        mixInput_stream1_rect_map.put("layer",0);
        mixInput_stream1_rect_map.put("top",0);
        mixInput_stream1_rect_map.put("left",0);
        mixInput_stream1_rect_map.put("bottom",640);
        mixInput_stream1_rect_map.put("right",480);

        LinkedHashMap<String,Object> mixInput_stream1_map = new LinkedHashMap<>();
        mixInput_stream1_map.put("url","");
        mixInput_stream1_map.put("stream_id",inputStreamId1);
        mixInput_stream1_map.put("rect",mixInput_stream1_rect_map);

        LinkedHashMap<String,Object> mixInput_stream2_rect_map = new LinkedHashMap<>();
        mixInput_stream2_rect_map.put("layer",1);
        mixInput_stream2_rect_map.put("top",480);
        mixInput_stream2_rect_map.put("left",320);
        mixInput_stream2_rect_map.put("bottom",640);
        mixInput_stream2_rect_map.put("right",480);

        LinkedHashMap<String,Object> mixInput_stream2_map = new LinkedHashMap<>();
        mixInput_stream2_map.put("url","");
        mixInput_stream2_map.put("stream_id",inputStreamId2);
        mixInput_stream2_map.put("rect",mixInput_stream2_rect_map);

        LinkedHashMap[] mixInput_array = {mixInput_stream1_map,mixInput_stream2_map};
        return mixInput_array;
    }

    //构造mixoutput的数据(数组)
    public static LinkedHashMap[] getMixOutputData_array(String outputStreamId, String cdnURL){
        LinkedHashMap<String,Object> mixOutput_map = new LinkedHashMap<>();
        mixOutput_map.put("stream_id", outputStreamId);
        mixOutput_map.put("mixurl", cdnURL + outputStreamId);
        mixOutput_map.put("bitrate", 400000);
        mixOutput_map.put("fps", 15);
        mixOutput_map.put("height", 640);
        mixOutput_map.put("width", 480);
        LinkedHashMap[] mixOutput_array = {mixOutput_map};
        return mixOutput_array;
    }

    public static void main(String args[]) throws Exception{

        // zego分配的appid
        Long appid = 2111975251L;
        // 发起混流的id_name
        String id_name = "fanzhi";
        // 混流的roomid
        String live_channel = "12";
        // 输入流的流id
        String inputStreamId1 = "zegotest-"+appid+ "-is1"; // 注意如果为测试环境,流id为 "zegotest-" + appid + "-is1"
        String inputStreamId2 = "zegotest-"+appid+ "-is2"; // 注意如果为测试环境,流id为 "zegotest-" + appid + "-is2"
        // 输出的混流流id
        String outputStreamId = "zegotest-" +appid+ "-os"; //注意如果为测试环境,流id为 "zegotest-" + appid + "-os"
        // cdn的推流地址
//        String cdnURL = "rtmp://127.0.0.1/xxx/";
        // md5加密所需的时间戳
        String timestamp = Long.toString(System.currentTimeMillis()/1000);
        // 请求混流的url,注意正式环境与测试环境的区别, url中包含的access_token参考 "获取Access Token"
        String startMixStream_url = "https://testwebapi.zego.im/cgi/start-mix?access_token=";

        // 构造生成mixInput_str的字符串和mixOutput_str的字符串, 需要做urlencode
        LinkedHashMap[] mixInput_array = getMixInputData_array(inputStreamId1, inputStreamId2);
        LinkedHashMap[] mixOutput_array = getMixOutputData_array(outputStreamId, null);

        LinkedHashMap<String,Object> postJsonData = new LinkedHashMap<>();
        postJsonData.put("id_name", id_name);
        postJsonData.put("live_channel", live_channel);
        postJsonData.put("appid", appid);
        postJsonData.put("MixInput", mixInput_array);
        postJsonData.put("MixOutput", mixOutput_array);

        String post_json_str = JSONObject.toJSONString(postJsonData);
//        System.out.println(post_json_str);
        String access_token = GetToken.getToken("2111975251", "009357ca836fa6109a88c6d9f0398262", "https://testwebapi.zego.im/cgi/token");
//        System.out.println("这位是access token"+access_token);
        // 发送请求开始混流, access_token 参考 "获取Access Token" 部分
        System.out.println(access_token);
        String ret = StartMixStream.sendPost(startMixStream_url + access_token, post_json_str);
        System.out.println(ret);
    }
}